package org.example.builder;

import org.apache.commons.lang3.ArrayUtils;
import org.example.bean.Constants;
import org.example.bean.FiledInfo;
import org.example.bean.TableInfo;
import org.example.utils.DateUtils;
import org.example.utils.JsonUtils;
import org.example.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class BuildQuery {
    private static final Logger logger = LoggerFactory.getLogger(BuildQuery.class);
    public static void execute(TableInfo tableInfo)  {

        File folder = new File(Constants.PATH_QUERY);

        if(!folder.exists()) {
            folder.mkdirs();
        }

        String className = tableInfo.getBeanName()+Constants.SUFFIX_BEAN_QUERY;
        File poFlie = new File(folder,className + ".java ");
        OutputStream out = null;
        OutputStreamWriter outw = null;
        BufferedWriter bw = null;
        try {

            out = new FileOutputStream(poFlie);
            outw = new OutputStreamWriter(out,"utf8");
            bw = new BufferedWriter(outw);
            bw.write("package " + Constants.PACKAGE_QUERY + ";");
            bw.newLine();
            bw.newLine();



            //导入时间日期相关的包
            if (tableInfo.getHaveDate() || tableInfo.getHaveDateTime()) {

//                bw.write(Constants.BEAN_DATE_FORMAT_CLASS);
//                bw.newLine();
//                bw.write(Constants.BEAN_DATE_UNFORMAT_CLASS);
//                bw.newLine();
//                bw.write("import " +Constants.PACKAGE_UTILS +".*;");
//                bw.newLine();
//                bw.write("import " + Constants.PACKAGE_ENUMS + ".*;");
//                bw.newLine();
                bw.newLine();
                bw.write("import java.util.Date;");
                bw.newLine();
            }


            if (tableInfo.getHaveBigDecimal()) {
                bw.write("import java.math.BigDecimal;");
                bw.newLine();
            }



            // 类上加注释
            BuildComment.createClassComment(bw,tableInfo.getComment()+"查询对象");

            bw.write("public class " + className+" extends BaseQuery {");
            bw.newLine();
            bw.newLine();

           // List<FiledInfo> fuzzyFieldList = new ArrayList<>();
            //拿到属性
            for (FiledInfo filed: tableInfo.getFiledList()) {
                //给每一个属性加上注释
                BuildComment.createFieldComment(bw, filed.getComment());
                bw.write( "\t"+"private " + filed.getJavaType() + " " + filed.getPropertyName() + ";");
                bw.newLine();
                bw.newLine();
                //String类型的属性加fuzzy
                if (ArrayUtils.contains(Constants.SQL_STRING_TYPE,filed.getSqlType())) {
                    String propertyName = filed.getPropertyName()+Constants.SUFFIX_BEAN_QUERY_FUZZY;
                    bw.write( "\t"+"private " + filed.getJavaType() + " " +propertyName + ";");
                    bw.newLine();
                    bw.newLine();


                }
                //日期类型的属性加 start end
                if (ArrayUtils.contains(Constants.SQL_DATE_TIME_TYPES,filed.getSqlType()) ||
                        ArrayUtils.contains(Constants.SQL_DATE_TYPES,filed.getSqlType())) {
                    String propertyStart = filed.getPropertyName()+Constants.SUFFIX_BEAN_QUERY_TIME_START;
                    String propertyEnd = filed.getPropertyName()+Constants.SUFFIX_BEAN_QUERY_TIME_END;
                    bw.write( "\t"+"private String" + " " + propertyStart + ";");
                    bw.newLine();
                    bw.newLine();
                    bw.write( "\t"+"private String" + " " + propertyEnd+ ";");
                    bw.newLine();
                    bw.newLine();


                }
            }

            List<FiledInfo> filedInfoList = new ArrayList<>();
            filedInfoList.addAll(tableInfo.getFiledList());
            filedInfoList.addAll(tableInfo.getFiledExtendsList());

            //加get set方法
            for (FiledInfo filed: filedInfoList) {
                String temp = StringUtils.upperCaseFirstLetter(filed.getPropertyName());
                bw.write("\t" + "public " + filed.getJavaType() + " get" + temp+"() {");
                bw.newLine();
                bw.write("\t\treturn " + filed.getPropertyName() +";");
                bw.newLine();
                bw.write("\t}");
                bw.newLine();

                bw.write("\t" + "public void " + " set" + temp+"(" + filed.getJavaType()+" " + filed.getPropertyName()+") {");
                bw.newLine();
                bw.write("\t\tthis."+filed.getPropertyName()+ " = " + filed.getPropertyName() +";");
                bw.newLine();
                bw.write("\t}");
                bw.newLine();

            }
            //重写tostring方法
//            StringBuffer toString = new StringBuffer();
//
//            for (FiledInfo filed: tableInfo.getFiledList()) {
//                String properName = filed.getPropertyName();
//                if(ArrayUtils.contains(Constants.SQL_DATE_TIME_TYPES,filed.getSqlType()) ) {
//                    properName = "DateUtil.format("+ filed.getPropertyName()+", DateTimePatternEnum.YYYY_MM_DD_HH_MM_SS.getPattern())";
//                } else if (ArrayUtils.contains(Constants.SQL_DATE_TYPES,filed.getSqlType()) ) {
//                    properName = "DateUtil.format("+ filed.getPropertyName()+", DateTimePatternEnum.YYYY_MM_DD.getPattern())";
//
//                }
//                toString.append("\", "+filed.getComment() + ":\"" +"+(" + filed.getPropertyName() + " == null ? " + "\"空\"" + " : " + properName+")+");
//
//
//            }
//            toString = toString.replace(1,3,"");
//            toString = toString.replace(toString.length() -1,toString.length(),"");
//
//            System.out.println(toString);
//            bw.write("\t@Override");
//            bw.newLine();
//            bw.write("\tpublic String toString() {");
//            bw.newLine();
//            bw.write("\t\treturn "+toString+";");
//            bw.newLine();
//            bw.write("\t}");
//            bw.newLine();
            bw.write("}");
            bw.flush();
        } catch (Exception e) {

            logger.error("创建po失败！",e);
        } finally {
            if(bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if(outw != null) {
                try {
                    outw.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if(out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

        }


    }
}
